package com.gdut.huayan.controller;

import com.gdut.huayan.entity.dto.PageQuery;
import com.gdut.huayan.entity.dto.Result;
import com.gdut.huayan.entity.po.Tablet;
import com.gdut.huayan.entity.vo.OrderCreateVo;
import com.gdut.huayan.entity.vo.TabletIdListVo;
import com.gdut.huayan.service.FileService;
import com.gdut.huayan.service.OrderService;
import com.gdut.huayan.validation.NoLog;
import com.gdut.huayan.validation.group.GroupAdd;
import com.gdut.huayan.validation.group.GroupUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.groups.Default;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

//    @Autowired
//    private FileService fileService;

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @PostMapping("/process")
    public Result processOrder(@RequestBody @Validated({Default.class, GroupAdd.class}) OrderCreateVo orderCreateVo){
        return Result.success(orderService.addStockOrder(orderCreateVo));
    }

    @NoLog
    @PreAuthorize("permitAll()")
    @PostMapping("/callback/hrk2ug328XCHfs24l")
    public Result callBack(HttpServletRequest httpServletRequest){
        orderService.callback(httpServletRequest);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @GetMapping("/continue")
    public Result continuePay(Long orderId){
        return Result.success(orderService.continuePay(orderId));
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/getAll")
    public Result listOrder(Integer status,
                            Integer isStatistics,
                            Integer isSuiXi,
                            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
                            @RequestBody PageQuery pageQuery){
        return Result.success(orderService.listOrder(pageQuery,status,isStatistics,startTime,endTime,isSuiXi));
    }

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @GetMapping("/detail")
    public Result getOrderDetail(Long orderId){
        return Result.success(orderService.getOrderDetail(orderId));
    }


    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @PostMapping("/get")
    public Result getUserOrder(@RequestBody PageQuery pageQuery, Integer status){
        return Result.success(orderService.getUserOrder(pageQuery,status));
    }

    // 返回用户常用牌位信息
    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @GetMapping("/tablet/getFrequentlyUsed")
    public Result getFrequentlyUsedTable(){
        return Result.success(orderService.getFrequentlyUsedTable());
    }

    // 修改常用牌位信息
    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @GetMapping("/tablet/updateFrequentlyUsed")
    public Result releaseFrequentlyUsedTablet(Long tabletId,Integer isSave){
        orderService.changeTabletIsSave(tabletId,isSave);
        return Result.success();
    }

    //管理员修改牌位
    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/tablet/update")
    public Result updateTablet(@RequestBody @Validated({Default.class, GroupUpdate.class}) Tablet tablet){
        orderService.updateTablet(tablet);
        return Result.success();
    }

    //管理员批量设置牌位打印信息
    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/tablet/setPrintStatus")
    public Result setPrintStatus(@RequestBody TabletIdListVo tabletIdListVo,Integer status){
        orderService.setPrintStatus(tabletIdListVo,status);
        return Result.success();
    }
}
